<script>
import { socket_url } from "@/utils/request.js";
import api from "./api/api";
import { log } from "./components/QS-SharePoster/app";
export default {
  onLaunch: function () {
    uni.hideTabBar()
    // console.log("App Launch");
    // #ifdef H5
    uni.setStorageSync("platform", "web");
    // #endif
    // #ifdef APP-PLUS
    const systemInfo = uni.getSystemInfoSync();
    uni.setStorageSync("platform", systemInfo.platform);
    // #endif
  },
  onShow: async function () {
    // console.log("App Show");
    // this.createSocket()

    this.storeUniqueCode();
  },
  onHide: function () {
    // console.log("App Hide");
  },
  methods: {
    storeUniqueCode() {
      // 全局拼接 unique code
      uni.removeStorageSync("unique_code");
      let uniqueStr;
      let mark_index = location.href.indexOf("#");
      if (mark_index) {
        uniqueStr = location.href.slice(0, mark_index);
      }

      if (uniqueStr[uniqueStr.length - 1] === "/") {
        uniqueStr = uniqueStr.slice(0, uniqueStr.length - 1);
      }

      uniqueStr = uniqueStr.slice(uniqueStr.lastIndexOf("/") + 1).trim();
      console.log("uniqueStr", uniqueStr);

      if (uniqueStr.indexOf("?")) {
        uniqueStr = uniqueStr.split("?")[0];
      }

      if (uniqueStr.length == 10) {
        uni.setStorageSync("unique_code", uniqueStr);
      }
    },
    // 创建链接
    createSocket() {
      uni.connectSocket({
        url: socket_url, //服务器接口地址
        success: (res) => {
          console.log("WebSocket连接成功", res);
          // this.listenerOpen();
          uni.setStorageSync("WebSocketID", res);
        },
        fail: (err) => {
          console.log("WebSocket连接失败", err);
          // uni.hideLoading();
          // uni.showToast({
          //   title: "设备初始化失败",
          //   icon: "none",
          // });
        },
      });
    },

    // 监听链接是否打开
    listenerOpen() {
      uni.onSocketOpen((res) => {
        // 读取聊天缓存
        this.$store.commit("chat/init_auto");

        this.receive(); //开启监听消息
        this.listenerError(); //开启监听错误
        this.listenerClose(); //开启监听关闭

        uni.sendSocketMessage({
          data: JSON.stringify({
            token: uni.getStorageSync("token"),
            type: "login",
            // role_type: uni.getStorageSync("identity")
            //   ? uni.getStorageSync("identity")
            //   : 1,
          }),
          success: (res) => {
            // 获取离线数据
            this.getOfflineMessages();
          },
          fail: (err) => {
            // console.log(err);
          },
        });

        if (this.interval) {
          clearInterval(this.interval);
          this.interval = null;
        }
        this.interval = setInterval(() => {
          uni.sendSocketMessage({
            data: '{"type":"pong"}',
          });
        }, 5000);
      });
    },

    // 监听链接关闭
    listenerClose() {
      uni.onSocketClose((res) => {
        if (uni.getStorageSync("token")) {
          this.getUserInfo();
          // this.createSocket();
        }
      });
    },
  },
};
</script>

<style lang="scss">
@import "uview-ui/index.scss";
@import "./style/yuanchuang.scss";
/*每个页面公共css */
.u-input__textarea {
  color: #999999 !important;
}
.u-input--border {
  padding: 30rpx !important;
  border-radius: 24rpx !important;
}
.u-mode-center-box {
  background-color: $bg-color1 !important;
}
.update {
  .u-mode-center-box {
    background-color: #ffffff !important;
  }
}
.u-drawer-bottom {
  background: transparent !important;
}
.online_list {
  .u-drawer-bottom {
    background: #ffffff !important;
  }
}
page {
  background-color: $bg-color1;
}
</style>
